<script setup>
import { ref, onMounted } from 'vue'
import PageFullContainer from '@/components/pageFullContainer.vue'
import { useUserStore } from '@/stores/user'
import {
  getPropertyListService,
  addHotPropertyService,
  deletePropertyService,
  searchPropertyService
} from '@/apis/property'
import router from '@/router'

const UserStore = useUserStore()

// 存储房源列表信息
const propertyList = ref([])

// 实现分页需要的参数
const pageSize = ref(5)
const page = ref(1)
const total = ref(null)

// 获取房源列表需要的参数
const propertyItem = ref({
  id: UserStore.UserList.id,
  pageSize: pageSize.value,
  page: page.value
})

// 获取房源列表信息
const getPropertyList = async () => {
  isLoading.value = true
  const res = await getPropertyListService(propertyItem.value)
  propertyList.value = res.data.propertyList.rows
  total.value = res.data.propertyList.count
  isLoading.value = false
}

onMounted(() => {
  getPropertyList()
})

// 控制loading加载的开启和关闭
const isLoading = ref(false)

// 处理页码变化
const handleCurrentChange = (newPage) => {
  propertyItem.value.page = newPage
  getPropertyList()
}

// 处理页面显示数据条数的变化
const handleSizeChange = (newSize) => {
  propertyItem.value.pageSize = newSize
  getPropertyList()
}

// 添加房源为热门房源
const addHotProperty = async (row) => {
  await addHotPropertyService(row.id)
  ElMessage.success('添加热门房源成功')
  getPropertyList()
}

// 编辑房源
const editorProperty = (row) => {
  router.push({
    path: '/property/publish',
    query: {
      id: row.id
    }
  })
}

// 删除房源
const deleteProperty = async (row) => {
  await ElMessageBox.confirm('你确定要删除该房源吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })

  // 删除房源
  await deletePropertyService(row.id)
  ElMessage.success('删除成功')
  getPropertyList()
}

// 搜索框内输入的内容
const SearchInput = ref(null)

// 搜索功能
const handleSearch = async () => {
  const res = await searchPropertyService(SearchInput.value)
  propertyList.value = res.data.data
}

// 重置功能
const handleReset = () => {
  SearchInput.value = null
  getPropertyList()
}
</script>

<template>
  <PageFullContainer title="房源列表">
    <template #extra>
      <div class="extra">
        <div class="search">
          <el-input
            v-model="SearchInput"
            style="width: 240px"
            placeholder="请输入搜索内容"
            @keyup.enter="handleSearch"
            clearable
          />
          <el-button type="primary" @click="handleSearch">
            <template #default>搜索</template>
            <template #icon>
              <el-icon><i-ep-Search></i-ep-Search></el-icon>
            </template>
          </el-button>
          <el-button type="info" @click="handleReset">
            <template #default>重置</template>
            <template #icon>
              <el-icon><i-ep-Refresh></i-ep-Refresh></el-icon>
            </template>
          </el-button>
        </div>
        <el-button
          type="primary"
          size="large"
          @click="$router.push('/property/publish')"
        >
          <template #default>添加房源</template>
          <template #icon>
            <el-icon><i-ep-Plus></i-ep-Plus></el-icon>
          </template>
        </el-button>
      </div>
    </template>

    <!-- 房源列表 -->
    <el-table
      :data="propertyList"
      style="width: 100%"
      v-loading="isLoading"
      v-if="propertyList.length"
    >
      <el-table-column prop="date" type="index" label="序号" width="60" />
      <el-table-column label="房源名称">
        <template #default="scope">
          <div class="ellipsis">
            {{ scope.row.title }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template #default="scope">
          <div class="image">
            <img
              v-if="scope.row.image_url.interior_img"
              :src="scope.row.image_url.interior_img[0].url"
              alt=""
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房源地址">
        <template #default="scope">
          <div class="ellipsis">
            {{ scope.row.address }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="户型">
        <template #default="scope">
          {{ scope.row.room_count }}室{{ scope.row.livingroom_count }}厅{{
            scope.row.bathroom_count
          }}卫
        </template>
      </el-table-column>
      <el-table-column prop="rent_type" label="租赁类型" />
      <el-table-column prop="decorate_type" label="装修" />
      <el-table-column prop="room_direction" label="朝向" />
      <el-table-column label="面积">
        <template #default="scope">
          {{ Math.floor(scope.row.room_area) }}
        </template>
      </el-table-column>
      <el-table-column label="房源价格">
        <template #default="scope">
          {{ Math.floor(scope.row.price) }}
        </template>
      </el-table-column>
      <el-table-column label="热门状态">
        <template #default="{ row }">
          <el-tag
            :type="row.is_hot === 1 ? 'success' : 'info'"
            effect="dark"
            size="large"
            round
            >{{ row.is_hot === 1 ? '热门房源' : '普通房源' }}</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="primary" circle @click="editorProperty(row)">
            <template #icon>
              <i-ep-Edit></i-ep-Edit>
            </template>
          </el-button>
          <el-button type="danger" circle @click="deleteProperty(row)">
            <template #icon>
              <i-ep-Delete></i-ep-Delete>
            </template>
          </el-button>
          <el-button
            v-if="row.is_hot === 0"
            type="success"
            circle
            @click="addHotProperty(row)"
          >
            <template #icon>
              <i-ep-Plus></i-ep-Plus>
            </template>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-empty v-else :image-size="200" />

    <template #footer>
      <!-- 分页区域 -->
      <el-pagination
        background
        layout="jumper,total,sizes,prev,pager,next"
        :page-sizes="[2, 3, 5, 10]"
        v-model:page-size="pageSize"
        v-model:current-page="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="total"
      ></el-pagination>
    </template>
  </PageFullContainer>
</template>

<style lang="scss" scoped>
.extra {
  width: 1250px;
  display: flex;
  justify-content: space-between;

  .search {
    button {
      margin: 0 10px;
    }
  }
}

.image {
  width: 100px;
  height: 75px;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>
